package com.hongkuncheng.cms.controller.admin;

import org.hibernate.LockMode;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.PageHelper;
import com.hongkuncheng.cms.model.VoteOption;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 网上投票选项
 *
 * @author 洪坤成
 */
@Controller
@Transactional
@RequestMapping("admin/voteoption")
public class AvoteoptionController extends BaseController {

    // 网上投票选项编辑
    @RequestMapping("edit")
    public String edit() {
        int voteoptionId = request.getParameter("voteoptionid") == null ? 0 :
                Integer.parseInt(request.getParameter("voteoptionid"));
        VoteOption voteoption = voteoptionId == 0 ? new VoteOption() : factorySession.get(VoteOption.class, voteoptionId);
        request.setAttribute("voteoption", voteoption);
        return view();
    }

    // 网上投票选项编辑处理
    @RequestMapping("editdo")
    @ResponseBody
    public void editdo() {
        checkAdminPermission("yunwei_plugin_vote");
        int voteoptionId = Integer.parseInt(request.getParameter("voteoptionid"));
        VoteOption voteoption = voteoptionId == 0 ? new VoteOption() :
                factorySession.load(VoteOption.class, voteoptionId, LockMode.PESSIMISTIC_WRITE);
        voteoption.setContents(request.getParameter("contents"));
        voteoption.setPoll(Integer.parseInt(request.getParameter("poll")));
        voteoption.setSort(Integer.parseInt(request.getParameter("sort")));
        if (voteoptionId == 0) factorySession.save(voteoption);
        recordLog((voteoptionId == 0 ? "新建" : "修改") + "投票项《" + voteoption.getContents().trim() + "》");
        PageHelper.alertAndRedirect("保存成功", "../vote/");
    }

    // 网上投票选项编辑删除
    @RequestMapping("delete")
    @ResponseBody
    public void delete() {
        checkAdminPermission("yunwei_plugin_vote");
        int voteoptionId = Integer.parseInt(request.getParameter("voteoptionid"));
        VoteOption voteoption = factorySession.load(VoteOption.class, voteoptionId, LockMode.PESSIMISTIC_WRITE);
        factorySession.delete(voteoption);
        recordLog("删除投票项《" + voteoption.getContents().trim() + "》");
        PageHelper.alertAndRedirect("删除成功", "../vote/");
    }

}
